/*
*  This file is part of mangusta
*
*  mangusta is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  mangusta is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with mangusta; if not, write to the Free Software
*  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  US
*
*  Author: <antonino.calderone@ericsson.com>, <acaldmail@gmail.com>
*
*/


/* -------------------------------------------------------------------------- */

#include "nu_variant.h"
#include "nu_functions.h"
#include "nu_os_std.h"
#include "nu_builtin_help.h"
#include "nu_os_gui.h"
#include "nu_lang_defs.h"

#include <functional>
#include <string>
#include <cstdlib>

#define _INT PROGLANG_TYPE_INTEGER
#define _STR PROGLANG_TYPE_STRING

/* -------------------------------------------------------------------------- */

namespace nu
{


/* -------------------------------------------------------------------------- */

#define THIS_MODULE "gui"
#define MD_NAMESPACE THIS_MODULE "."


/* -------------------------------------------------------------------------- */

static struct module_gui_t : public module_t
{
   module_gui_t()
   {
      add_module(this, THIS_MODULE);
   }

   virtual bool load() throw() override
   {
      auto & hmap = builtin_help_t::get_instance();

      hmap[ {lang_item_t::MODULE, THIS_MODULE}] =
      {
         "The " THIS_MODULE " module provides access to the GUI functions",
         THIS_MODULE" module"
      };

      functions_t & fmap = functions_t::get_instance();


      // -----------------------------------------------------------------------

      auto functor_textout = [](
                                rt_prog_ctx_t & ctx,
                                const std::string& name,
                                const nu::func_args_t & args)
      {
         std::vector< variant_t > vargs;

         functions_t::get_functor_vargs(
            ctx,
            name,
            args,
         {
            variant_t::type_t::INTEGER, //x
            variant_t::type_t::INTEGER, //y
            variant_t::type_t::STRING,  //text
            variant_t::type_t::INTEGER  //color
         },
         vargs);

         return gui_t::get_instance().textout(vargs);
      };

      hmap[ {lang_item_t::FUNCTION, MD_NAMESPACE "textout"}] =
      {
         "Writes a string at the specified location, using the currently selected font.\n"
         "Upon successful completion, 0 shall be returned; \n"
         "otherwise, error code shall be returned",
         _INT " " MD_NAMESPACE
         "textout("_INT" x, "_INT" y, " _STR " text, "_INT" textcol)"
      };

      fmap[MD_NAMESPACE "textout"] = functor_textout;


      // -----------------------------------------------------------------------

      auto functor_line = [](
                             rt_prog_ctx_t & ctx,
                             const std::string& name,
                             const nu::func_args_t & args)
      {
         std::vector< variant_t > vargs;

         std::vector< variant_t::type_t > vtype(6);
         std::fill(vtype.begin(), vtype.end(), variant_t::type_t::INTEGER);

         functions_t::get_functor_vargs( ctx, name, args, vtype, vargs);

         return gui_t::get_instance().line(vargs);
      };

      hmap[ {lang_item_t::FUNCTION, MD_NAMESPACE "line"}] =
      {
         "Draws a line connecting the two points specified by the coordinate pairs\n"
         "Upon successful completion, 0 shall be returned; \n"
         "otherwise, error code shall be returned",
         _INT " " MD_NAMESPACE
         "line("_INT" x1, "_INT" y1, "_INT" x2, "_INT" y2, "_INT" col, "_INT" width)"
      };

      fmap[MD_NAMESPACE "line"] = functor_line;


      // -----------------------------------------------------------------------

      auto functor_rect = [](
                             rt_prog_ctx_t & ctx,
                             const std::string& name,
                             const nu::func_args_t & args)
      {
         std::vector< variant_t > vargs;

         //x,y,w,h,color,linewidth
         std::vector< variant_t::type_t > vtype(6);
         std::fill(vtype.begin(), vtype.end(), variant_t::type_t::INTEGER);

         functions_t::get_functor_vargs(ctx, name, args, vtype, vargs);

         bool fillrect = name == MD_NAMESPACE "fillrect";

         return gui_t::get_instance().rect(vargs, fillrect);
      };

      hmap[ {lang_item_t::FUNCTION, MD_NAMESPACE "rect"}] =
      {
         "Draws a rectangle specified by a coordinate pair, a width, and a height\n"
         "Upon successful completion, 0 shall be returned; \n"
         "otherwise, error code shall be returned",
         _INT " " MD_NAMESPACE
         "rect("_INT" x, "_INT" y, "_INT" w, "_INT" h, "_INT" color, "_INT" width)"
      };

      fmap[MD_NAMESPACE "rect"] = functor_rect;


      hmap[ {lang_item_t::FUNCTION, MD_NAMESPACE "fillrect"}] =
      {
         "Draws a fill rectangle specified by a coordinate pair, a width, and a height\n"
         "Upon successful completion, 0 shall be returned; \n"
         "otherwise, error code shall be returned",
         _INT " " MD_NAMESPACE
         "fillrect("_INT" x, "_INT" y, "_INT" w, "_INT" h, "_INT" col, "_INT" width)"
      };

      fmap[MD_NAMESPACE "fillrect"] = functor_rect;


      // -----------------------------------------------------------------------

      auto functor_ellipse = [](
                                rt_prog_ctx_t & ctx,
                                const std::string& name,
                                const nu::func_args_t & args)
      {
         std::vector< variant_t > vargs;

         //x,y,w,h,color,linewidth
         std::vector< variant_t::type_t > vtype(6);
         std::fill(vtype.begin(), vtype.end(), variant_t::type_t::INTEGER);

         functions_t::get_functor_vargs(ctx, name, args, vtype, vargs);

         bool fillellipse = name == MD_NAMESPACE "fillellipse";

         return gui_t::get_instance().ellipse(vargs, fillellipse);
      };

      hmap[ {lang_item_t::FUNCTION, MD_NAMESPACE "ellipse"}] =
      {
         "Draws an ellipse within a bounding rectangle specified by a \n"
         "coordinate pair, a width, and a height\n"
         "Upon successful completion, 0 shall be returned; \n"
         "otherwise, error code shall be returned",
         _INT " " MD_NAMESPACE
         "ellipse("_INT" x, "_INT" y, "_INT" w, "_INT" h, "_INT" col, "_INT" width)"
      };

      fmap[MD_NAMESPACE "ellipse"] = functor_ellipse;


      hmap[ {lang_item_t::FUNCTION, MD_NAMESPACE "fillellipse"}] =
      {
         "Draws an filled-ellipse within a bounding rectangle specified by a \n"
         "coordinate pair, a width, and a height\n"
         "Upon successful completion, 0 shall be returned; \n"
         "otherwise, error code shall be returned",
         _INT " " MD_NAMESPACE
         "fillellipse("_INT" x, "_INT" y, "_INT" w, "_INT" h, "_INT" col, "_INT" width)"
      };

      fmap[MD_NAMESPACE "fillellipse"] = functor_ellipse;


      // -----------------------------------------------------------------------

      auto functor_circle = [](
                               rt_prog_ctx_t & ctx,
                               const std::string& name,
                               const nu::func_args_t & args)
      {
         std::vector< variant_t > vargs;

         //x,y,radius,color,linewidth
         std::vector< variant_t::type_t > vtype(5);
         std::fill(vtype.begin(), vtype.end(), variant_t::type_t::INTEGER);

         functions_t::get_functor_vargs(ctx, name, args, vtype, vargs);

         bool fillcircle = name == MD_NAMESPACE "fillcircle";

         return gui_t::get_instance().circle(vargs, fillcircle);
      };

      hmap[ {lang_item_t::FUNCTION, MD_NAMESPACE "circle"}] =
      {
         "Draws a circle specified by a coordinate pair and a radius\n"
         "Upon successful completion, 0 shall be returned; \n"
         "otherwise, error code shall be returned",
         _INT " " MD_NAMESPACE
         "circle("_INT" x, "_INT" y, "_INT" r, "_INT" color, "_INT" linewidth)"
      };

      fmap[MD_NAMESPACE "circle"] = functor_circle;


      hmap[ {lang_item_t::FUNCTION, MD_NAMESPACE "fillcircle"}] =
      {
         "Draws a filled circle specified by a coordinate pair and a radius\n\n"
         "Upon successful completion, 0 shall be returned; \n"
         "otherwise, error code shall be returned",
         _INT " " MD_NAMESPACE
         "fillcircle("_INT" x, "_INT" y, "_INT" r, "_INT" col, "_INT" width)"
      };

      fmap[MD_NAMESPACE "fillcircle"] = functor_circle;


      // -----------------------------------------------------------------------


      //TODO
      hmap[ {lang_item_t::FUNCTION, "getmousex"}] =
      {
         "Returns the current X position of the mouse. "
         "X is measured in pixels, starting at 0 for "
         "the left side of the working window.",
         "GetMouseX()"
      };


      hmap[ {lang_item_t::FUNCTION, "getmousey"}] =
      {
         "Returns the current Y position of the mouse. "
         "Y is measured in pixels, starting at 0 for "
         "the top of the working window.",
         "GetMouseY()"
      };


      hmap[ {lang_item_t::FUNCTION, "getmousebtn"}] =
      {
         "Returns whether the given mouse button is held down. "
         "Button values are 1 for left button, 4 for right button, "
         "2 for the middle button. ",
         "GetMouseBtn()"
      };

      return true;
   }

}
module_instance;


/* -------------------------------------------------------------------------- */

} // namespace nu

